/*
 * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CONTAINER_BUNDLE_H
#define _CONTAINER_BUNDLE_H
#ifdef LOSCFG_KERNEL_CONTAINER

#include "los_process_pri.h"
#ifdef LOSCFG_PID_CONTAINER
#include "los_pid_container.h"
#endif
#include "asm/atomic.h"

#ifdef LOSCFG_UTS_CONTAINER
struct UtsContainer;
#endif
#ifdef LOSCFG_MNT_CONTAINER
struct MntContainer;
#endif
#ifdef LOSCFG_NET_CONTAINER
struct NetContainer;
#endif
#ifdef LOSCFG_PID_CONTAINER
struct PidContainer;
#endif
#ifdef LOSCFG_IPC_CONTAINER
struct IpcContainer;
#endif

typedef struct ContainerBundle {
    atomic_t count;
#ifdef LOSCFG_UTS_CONTAINER
    struct UtsContainer *utsContainer;
#endif
#ifdef LOSCFG_MNT_CONTAINER
    struct MntContainer *mntContainer;
#endif
#ifdef LOSCFG_NET_CONTAINER
    struct NetContainer *netContainer;
#endif
#ifdef LOSCFG_PID_CONTAINER
    struct PidContainer *pidContainer;
    struct PidContainer *pidContainerForChildren;
#endif
#ifdef LOSCFG_TIME_CONTAINER
        struct TimeContainer *timeContainer;
        struct TimeContainer *timeContainerForChildren;
#endif

#ifdef LOSCFG_IPC_CONTAINER
    struct IpcContainer *ipcContainer;
#endif
} ContainerBundle;

extern ContainerBundle g_rootContainerBundle;

extern void *ProcfsContainerGet(struct Vnode *vnode);

extern int ProcfsContainerTypeGet(struct Vnode *vnode);

void InitContainerBundle(void);

int CopyContainers(unsigned long flags,  LosProcessCB *childProcessCB, LosProcessCB *runProcessCB);

ContainerBundle *CopyContainerBundleOnly(LosProcessCB *processCB);

int PlaceProcessIntoContainers(LosProcessCB *childProcessCB);

void FreeContainerBundle(ContainerBundle *pContainer);

static inline void DerefContainerBundle(ContainerBundle *pContainer)
{
    if (pContainer && atomic_dec_and_test(&pContainer->count)) {
        FreeContainerBundle(pContainer);
    }
}

static inline void GetContainerBundle(ContainerBundle *pContainer)
{
    if (pContainer) {
        atomic_inc(&pContainer->count);
    }
}

void ExitProcessContainer(LosProcessCB *task);

int UnshareContainers(unsigned long flags, ContainerBundle **bundle);

void AssignContainers(LosProcessCB *process, ContainerBundle *newBundle);

void *GetTargetContainer(INT32 fd);

INT32 GetTargetContainerType(INT32 fd, INT32 type);

#endif
#endif /* _CONTAINER_BUNDLE_H */

